package com.nononsenseapps.feeder.db.view

import androidx.room.ColumnInfo
import androidx.room.DatabaseView
import androidx.room.Ignore
import com.nononsenseapps.feeder.db.FEED_ITEM
import com.nononsenseapps.feeder.db.entity.ATTENTION_NORMAL
import com.nononsenseapps.feeder.db.room.ID_UNSET
import com.nononsenseapps.feeder.db.entity.PROCESS_STATE_NO_SET
import com.nononsenseapps.feeder.db.entity.domain
import com.nononsenseapps.feeder.db.entity.enclosureFilename
import com.nononsenseapps.feeder.util.sloppyLinkToStrictURLNoThrows
import io.ktor.http.Url
import kotlinx.datetime.LocalDateTime


/*const val previewColumns = "feed_items.id AS id, guid, plain_title, plain_snippet, feed_items.image_url, feed_items.image_count AS image_count, feed_items.item_tags AS item_tags, enclosure_link, author, pub_date, link, unread, feeds.tag AS tag, feeds.id AS feed_id, feeds.title AS feed_title, feeds.custom_title as feed_customtitle, feeds.url AS feed_url,item_feature,feeds.image_url AS feed_thumb, word_count ," +
        FEED_ITEM_PRELOAD_ITEM.tableColumn(FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE)+" as "+FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE*/


/*const val previewHistoryColumns = "feed_items.id AS id, guid, plain_title, plain_snippet, feed_items.image_url, feed_items.image_count AS image_count, feed_items.item_tags AS item_tags, enclosure_link, " +
        "author, read_history AS pub_date, link, unread, feeds.tag AS tag, feeds.id AS feed_id, feeds.title AS feed_title, feeds.custom_title as feed_customtitle, feeds.url AS feed_url,item_feature,feeds.image_url AS feed_thumb, word_count ,${FEED_ITEM.COL_PRE_LOAD_STATE} "*/

/*@DatabaseView("""
        SELECT feed_items.id AS id,
               guid,
               plain_title,
               plain_snippet,
               feed_items.image_url AS image_url,
               feed_items.image_count AS image_count,
               item_tags ,
               enclosure_link,
               author,
               pub_date,
               link,
               unread,
               feeds.tag AS tag,
               feeds.id AS feed_id,
               feeds.title AS feed_title,
               feeds.custom_title as feed_customize_title,
               feeds.url AS feed_url,
               item_feature,
               feeds.image_url AS feed_thumb,
               word_count ,
               feed_item_preload_item.state AS state ,
               read_history,
               feed_items.attention_level AS attention_level
        FROM feed_items
        LEFT JOIN feeds ON feed_items.feed_id = feeds.id
        LEFT JOIN feed_item_preload_item ON feed_items.id = feed_item_preload_item.feed_item_id AND feed_item_preload_item.is_enclosure = 1
        """, viewName = "feed_item_view"
)*/

@DatabaseView("""
        SELECT feed_items.id AS id, 
               guid, 
               plain_title, 
               plain_snippet, 
               feed_items.image_url AS image_url, 
               feed_items.image_count AS image_count, 
               item_tags , 
               enclosure_link, 
               author, 
               pub_date, 
               link, 
               unread, 
               feeds.tag AS tag, 
               feeds.id AS feed_id, 
               feeds.title AS feed_title, 
               feeds.custom_title as feed_customize_title, 
               feeds.url AS feed_url,
               feeds.image_pre_download AS image_pre_download,
               item_feature,
               feeds.image_url AS feed_thumb, 
               word_count,
               read_history,
               feed_items.attention_level AS attention_level,
               create_date
        FROM feed_items
        LEFT JOIN feeds ON feed_items.feed_id = feeds.id
        """, viewName = "feed_item_view"
)
data class PreviewItem @Ignore constructor(
    var id: Long = ID_UNSET,
    var guid: String = "",
    @ColumnInfo(name = "plain_title") var plainTitle: String = "",
    @ColumnInfo(name = "plain_snippet") var plainSnippet: String = "",
    @ColumnInfo(name = "image_url") var imageUrl: String? = null,
    @ColumnInfo(name = "image_count") var image_count: Int = 0,
    @ColumnInfo(name = "enclosure_link") var enclosureLink: String? = null,
    var author: String? = null,
    @ColumnInfo(name = "pub_date") var pubDate: LocalDateTime? = null,
    var link: String? = null,
    var tag: String = "",
    var unread: Boolean = true,
    @ColumnInfo(name = "feed_id") var feedId: Long? = null,
    @ColumnInfo(name = "feed_title") var feedTitle: String = "",
    @ColumnInfo(name = "feed_customize_title") var feedCustomTitle: String = "",
    @ColumnInfo(name = "feed_url") var feedUrl: Url = sloppyLinkToStrictURLNoThrows(""),
    @ColumnInfo(name = "item_tags") var itemTags: String = "",
    @ColumnInfo(name = "item_feature")  var feature: Long? = null,
    @ColumnInfo(name = "feed_thumb") var feedThumb: String?=null,
    @ColumnInfo(name = "word_count") var wordCount: Int = 0,
    @ColumnInfo(name = "attention_level") var attentionLevel: Int = ATTENTION_NORMAL,
    @ColumnInfo(name = "read_history", typeAffinity = ColumnInfo.TEXT) var readHistory: LocalDateTime? = null,
    @ColumnInfo(name = "image_pre_download") var imagePreLoad: Boolean = false,
    @ColumnInfo(name = "create_date", typeAffinity = ColumnInfo.INTEGER) var createDate: LocalDateTime ? = null
) {
    constructor() : this(id = ID_UNSET)

    val feedDisplayTitle: String
        get() = feedCustomTitle.ifBlank { feedTitle }

    val enclosureFilename: String?
        get() = enclosureFilename(enclosureLink)

    val pubDateString: String?
        get() = pubDate?.toString()

    val domain: String?
        get() = domain(enclosureLink)

    fun tags():List<String>{
        return itemTags.split(",")
    }
}
